#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from CheckExcel import CheckExcel
from MakeXML import MakeXML
from Logger import logger
import os
import sys


xml_root_name_dict = {
    'activity.xlsx': 'activity',
    'objects.xlsx': 'objects',
    'shop.xlsx': 'config',
    'disgift.xlsx': 'disgift',
    'giftbox.xlsx': 'config',
    'drops.xlsx': 'drops',
    'sequence.xlsx': 'config',
    'ShopConfig.xlsx': 'Config',
    'ActivityConfig.xlsx': 'Config',
    'PreviewConfig.xlsx': 'Config',
    'TimeShopConfig.xlsx': 'Config',
}


root_path = os.path.dirname(os.path.abspath(__file__))
for root, dirs, files in os.walk(root_path):
    for file in files:
        if file in xml_root_name_dict:
            excel_path = os.path.join(root, file)
            xml_name = str(file.split('.')[0]) + '.xml'
            xml_path = os.path.join(root, xml_name)
            excel_obj = CheckExcel(excel_path)
            xml_root_name = xml_root_name_dict[excel_obj.file_name]
            xml_obj = MakeXML(xml_root_name, xml_path)
            attr_group = excel_obj.attr_group

            # 检查重复ID
            if excel_obj.file_name == 'ShopConfig.xlsx':
                try:
                    excel_obj.check_dup_id(xpath='>>Config/BallEquipment/item')
                except Exception:
                    logger.error('工作簿[{0}]检测重复ID失败'.format(excel_path))

            # 检查重复ID
            elif excel_obj.file_name == 'objects.xlsx':
                try:
                    excel_obj.check_dup_id(xpath='>>objects/item')
                except Exception:
                    logger.error('工作簿[{0}]检测重复ID失败'.format(excel_path))

            # 生成Xml表
            try:
                xml_obj.make_xml(attr_group)
                xml_obj.make_xml_string()
                xml_obj.write_xml_file()
            except Exception:
                logger.error('工作簿[{0}]生成xml失败'.format(excel_path))
            else:
                logger.info('工作簿[{0}]生成xml成功'.format(excel_path))

print('')
print('-' * 100)
string = input('请按任意键关闭控制台')
if string:
    sys.exit(0)
